home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / glx / font.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  299 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * 1993 Simon Hui -- Silicon Graphics Computer Systems
  19.  */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <GL/gl.h>
  24. #include <GL/glx.h>
  25. #include <GL/glu.h>
  26. #include <X11/keysym.h>
  27. #include "util.h"
  28.  
  29. #define W 600
  30. #define H 600
  31. #define FONT1 \
  32. "-adobe-itc avant garde gothic-demi-o-normal--25-180-100-100-p-139-iso8859-1"
  33. #define FONT2 "10x20"
  34.  
  35. /*
  36. ** The base for display list of entire font.
  37. */
  38. #define FONTBASE 0x1000
  39. /*
  40. ** The base for display list of just the alphabet.
  41. */
  42. #define ALPHABETBASE 0x8000
  43.  
  44. #define DRAW_ALL 1
  45. #define DRAW_ALPHABET 2
  46. #define DRAW_RAINBOW 3
  47.   
  48. static long width = W, height = H;
  49. GC xgc;
  50. Display *dpy;
  51. Colormap cmap;
  52. Window win;
  53. GLXContext cx;
  54. XFontStruct *fontStruct;
  55. XVisualInfo *vi;
  56.   
  57. char *theString = "OpenGL";
  58. char *fontName;
  59.  
  60. int mode = DRAW_RAINBOW;
  61. int glyphs, rows, cols, first, last;
  62. int firstRow, lastRow;
  63.  
  64. static void Init(void)
  65. {
  66.     XGCValues gcvalues;
  67.     int fg;
  68.  
  69.     fontStruct = XLoadQueryFont(dpy, FONT1); 
  70.     if (!fontStruct) {
  71.     /* if the avant-garde font is not avail, try the simpler 10x20 font */
  72.     fontStruct = XLoadQueryFont(dpy, FONT2); 
  73.     if (!fontStruct) {
  74.         fprintf(stderr, "font %s not found\n", FONT2);
  75.         exit(1);
  76.     }
  77.     }
  78.     firstRow = fontStruct->min_byte1;
  79.     lastRow = fontStruct->max_byte1;
  80.     rows = lastRow - firstRow + 1;
  81.     if (rows > 5) {
  82.     printf ("only using first 5 rows of this multi-row font\n");
  83.     rows = 5;
  84.     lastRow = firstRow + rows - 1;
  85.     }
  86.     first = fontStruct->min_char_or_byte2;
  87.     last = fontStruct->max_char_or_byte2;
  88.     cols = last - first + 1;
  89.     glyphs = cols;
  90.     
  91.     /*
  92.     ** Define bitmaps for entire font.
  93.     */
  94.     glXUseXFont(fontStruct->fid, firstRow << 8, rows*256, FONTBASE);
  95.  
  96.     /*
  97.     ** Define bitmaps for just the alphabet.
  98.     */
  99.     glXUseXFont(fontStruct->fid, 'A', 26, ALPHABETBASE);
  100.  
  101.     gcvalues.foreground = utilXPixel(0, 0, 0, vi);
  102.     gcvalues.background = utilXPixel(0, 1, 0, vi);
  103.     gcvalues.font = fontStruct->fid;
  104.     XChangeGC(dpy, xgc, GCForeground|GCBackground|GCFont, &gcvalues);
  105.     XDrawString(dpy, win, xgc, W/2, H/2, theString, strlen(theString));
  106.     XFlush(dpy);
  107.  
  108.     glMatrixMode(GL_PROJECTION);
  109.     glLoadIdentity();
  110.     glOrtho(-0.1, 1.1, -0.1, 1.1, 0, 1);
  111.     glMatrixMode(GL_MODELVIEW);
  112.     glClearColor(1.0, 0.0, 1.0, 0.0);
  113. }
  114.  
  115. static void DrawAll(void)
  116. {
  117.     int r;
  118.     int i, j, c;
  119.     int nrow, ncol=16;
  120.     char buf[20];
  121.  
  122.     glColor3f(0.0, 0.0, 1.0);
  123.     for (r=0; r < rows; r++) {
  124.     glClear( GL_COLOR_BUFFER_BIT );
  125.     c = FONTBASE + (r<<8);
  126.     for (j=15; j > -1; j--) {
  127.         for (i=0; i < 16; i++) {
  128.         glRasterPos2f((float)i/16.0, (float)j/16.0);
  129.         glCallList(c);
  130.         c++;
  131.         }
  132.     }
  133.     glFlush();
  134.     if (rows > 1) {
  135.         printf( "hit return to see next row of font>" );
  136.         gets(buf);
  137.     }
  138.     }
  139. }
  140.  
  141. static void DrawAlphabet(void)
  142. {
  143.     int i, j, c;
  144.     int nrow=16, ncol=16;
  145.  
  146.     glColor3f(1.0, 0.0, 0.0);
  147.     for (j=0; j < nrow; j++) {
  148.     for (i=0; i < ncol; i++) {
  149.         glRasterPos2f((float)i/ncol, (float)j/nrow);
  150.         c = (j*ncol + i) % 26;
  151.         glCallList(ALPHABETBASE + c);
  152.     }
  153.     }
  154. }
  155.  
  156. static float colors[][3] = {
  157.     {1, 0, 0},
  158.     {1, 1, 0},
  159.     {0, 1, 1},
  160.     {1, 0, 1},
  161.     {0, 0, 1},
  162. };
  163.  
  164. void DrawRainbow(void)
  165. {
  166.     int i, ncolors, nrows, h;
  167.     float *this, *next;
  168.     float r, g, b;
  169.     float x, y, dx, dy;
  170.  
  171.     h = fontStruct->max_bounds.ascent + fontStruct->max_bounds.descent;
  172.     ncolors = sizeof(colors) / sizeof(colors[0]);
  173.     nrows = 10;
  174.     dx = 1.0 / ncolors;
  175.     dy = 1.0 / nrows;
  176.  
  177.     glListBase(FONTBASE);
  178.     x = 0;
  179.     for (i=0; i < ncolors; i++) {
  180.     for (y=0; y < 1; y+=dy) {
  181.         this = colors[i];
  182.         next = colors[(i+1)%ncolors];
  183.         r = (y*next[0]) + (1-y)*this[0];
  184.         g = (y*next[1]) + (1-y)*this[1];
  185.         b = (y*next[2]) + (1-y)*this[2];
  186.         glColor3f(r, g, b);
  187.         glRasterPos2f(x, y);
  188.         glCallLists(strlen(theString), GL_BYTE, (GLubyte *)theString);
  189.     }
  190.     x += dx;
  191.     }
  192.     XDrawString(dpy, win, xgc, W/2, H/2, theString, strlen(theString));
  193. }
  194.  
  195. void DoDisplay(void)
  196. {
  197.     glClearColor(.5, .5, .5, 1);
  198.     glClear(GL_COLOR_BUFFER_BIT);
  199.     
  200.     switch(mode) {
  201.       case DRAW_ALL:
  202.     DrawAll();
  203.     break;
  204.       case DRAW_ALPHABET:
  205.     DrawAlphabet();
  206.     break;
  207.       case DRAW_RAINBOW:
  208.     DrawRainbow();
  209.     break;
  210.     }
  211.     glFlush();
  212. }
  213.  
  214. static int attributes[] = {
  215.     GLX_RGBA,
  216.     GLX_RED_SIZE, 1,
  217.     GLX_GREEN_SIZE, 1,
  218.     GLX_BLUE_SIZE, 1,
  219.     None,
  220. };
  221.  
  222. static void usage(void)
  223. {
  224.     printf("Usage: font [-f fontname] [-s string]\n");
  225.     exit(-1);
  226. }
  227.  
  228. int main(int argc, char **argv)
  229. {
  230.     XEvent event;
  231.     int i;
  232.  
  233.     for (i = 1; i < argc; i++) {
  234.     if (argv[i][0] == '-') {
  235.         switch(argv[i][1]) {
  236.           case 'f':
  237.         fontName = argv[++i];
  238.         break;
  239.           case 's':
  240.         theString = argv[++i];
  241.         break;
  242.           default:
  243.         usage();
  244.         break;
  245.         }
  246.     } else {
  247.         usage();
  248.     }
  249.     }
  250.     win = utilCreateWindow( width, height, 100, 100, None, attributes, "Gray",
  251.                 argc, argv, NULL, &dpy, &vi, &cmap, &xgc );
  252.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  253.     if (!glXMakeCurrent(dpy, win, cx)) {
  254.     fprintf(stderr, "Can't make window current to context\n");
  255.     return -1;
  256.     }
  257.     Init();
  258.     for (;;) {
  259.     do {
  260.         XNextEvent(dpy, &event);
  261.         switch (event.type) {
  262.           case Expose:
  263.         DoDisplay();
  264.         break;
  265.           case KeyPress:
  266.         {
  267.             char buf[100];
  268.             int rv;
  269.             KeySym ks;
  270.  
  271.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  272.             switch (ks) {
  273.               case XK_a:
  274.             mode = DRAW_ALL;
  275.             DoDisplay();
  276.             break;
  277.               case XK_b:
  278.             mode = DRAW_ALPHABET;
  279.             DoDisplay();
  280.             break;
  281.               case XK_r:
  282.             mode = DRAW_RAINBOW;
  283.             DoDisplay();
  284.             break;
  285.               case XK_Escape:
  286.             goto done;
  287.             break;
  288.             }
  289.         }
  290.         break;
  291.         }
  292.     } while (XPending(dpy) != 0);
  293.     }
  294.   done:
  295.     glXMakeCurrent(dpy, None, NULL);
  296.     glXDestroyContext(dpy, cx);
  297.     XCloseDisplay(dpy);
  298. }
  299.